package com.jelly.dto;

import java.io.Serializable;

import com.jelly.constant.ResultCode;

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
public class Result<T> implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8420261512974629108L;

	private T data;

	private Meta meta;

	private Result() {

	}

	public static <T> Result<T> success() {
		Result<T> result = new Result<>();
		result.setMeta(new Meta(ResultCode.SUCCESS));
		result.setData(null);
		return result;
	}

	public static <T> Result<T> success(T data) {
		Result<T> result = new Result<>();
		result.setMeta(new Meta(ResultCode.SUCCESS));
		result.setData(data);
		return result;
	}

	public static <T> Result<T> error() {
		Result<T> result = new Result<T>();
		result.setMeta(new Meta(ResultCode.INTERNAL_ERROR));
		return result;
	}

	public static <T> Result<T> error(String message) {
		Result<T> result = new Result<T>();
		result.setMeta(new Meta(ResultCode.INTERNAL_ERROR.code(), message));
		return result;
	}

	public static <T> Result<T> error(Integer code, String message) {
		Result<T> result = new Result<T>();
		result.setMeta(new Meta(code, message));
		return result;
	}

	public static <T> Result<T> error(ResultCode resultCode) {
		Result<T> result = new Result<T>();
		result.setMeta(new Meta(resultCode));
		return result;
	}

	public Result(T data, ResultCode resultCode) {
		super();
		this.data = data;
		this.meta = new Meta(resultCode);
	}

	@Data
	static class Meta {

		private String msg;

		private int status;

		public Meta(ResultCode resultCode) {
			this.msg = resultCode.message();
			this.status = resultCode.code();
		}

		public Meta(int status, String msg) {
			this.msg = msg;
			this.status = status;
		}

		public Meta() {

		}

	}
}